﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.IO;


public partial class back_AddArchitects : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Architect architect;
    ArchitecturalWork architectWork;
    List<ArchitecturalWork> architectWorks = new List<ArchitecturalWork>();
    State state;
    Country country;

    List<Country> countries = new List<Country>();
    List<State> states = new List<State>();
    List<City> cities = new List<City>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["admin"] != null)
            {
                //SetInitialRowForArchitecturalWorks();
                //SetInitialRowForArchitecturalFirms();
                //bindCountry();
            }
            else
                Response.Redirect("login.aspx");

            if (Request.QueryString != null)
            {
                if (Request.QueryString["Id"] != null)
                    bindArchitects(Request.QueryString["Id"].ToString());
            }
        }
    }
    private void bindArchitects(string architectId)
    {
        architect = new Architect(_dataObject);
        architect.Id = Convert.ToInt32(architectId);
        architect.GetArchitectById();

        if (architect.Id != 0)
        {
            txtFirmName.Text = architect.Firm;
            hfArchitectImageName.Value = architect.Logo;
            txtAddress.Text = architect.AddressLine;
            txtPhone.Text = architect.Phone;
            txtFax.Text = architect.Fax;
            txtEmail.Text = architect.Email;
            txtWebsite.Text = architect.Website;
            ddlPropertyLocation.SelectedValue = architect.PropertyLocation;
            txtDescription.Text = architect.DescriptionLine;
            txtEmployeeNos.Text = architect.NoOfEmployees;
            txtFounded.Text = architect.FoundedYear;
            txtSpecialization.Text = architect.Specialization;
            txtUsername.Text = architect.Username;
            txtUsername.Enabled = false;
            txtPassword.Attributes.Add("value", architect.Password);

            //architectFirm = new ArchitecturalFirm(_dataObject);
            //architectFirms = new List<ArchitecturalFirm>();
            //architectFirms = architectFirm.GetAllFirmsByArchitectId(architect.Id);
            //if (architectFirms.Count > 0)
            //{
            //    grdArchitecturalFirms.DataSource = architectFirms;
            //    grdArchitecturalFirms.DataBind();
            //}

            //architectWork = new ArchitecturalWork(_dataObject);
            //architectWorks = new List<ArchitecturalWork>();
            //architectWorks = architectWork.GetAllWorksByArchitectId(architect.Id);
            //if (architectWorks.Count > 0)
            //{
            //    grdArchitecturalWorks.DataSource = architectWorks;
            //    grdArchitecturalWorks.DataBind();
            //}
        }
    }
    private void SetInitialRowForArchitecturalFirms()
    {
        //List<ArchitecturalFirm> aFirms = new List<ArchitecturalFirm>();
        //for (int i = 0; i < 3; i++)
        //{
        //    architectFirm = new ArchitecturalFirm();
        //    aFirms.Add(architectFirm);
        //}
        //grdArchitecturalFirms.DataSource = aFirms;
        //grdArchitecturalFirms.DataBind();
    }
    private void SetInitialRowForArchitecturalWorks()
    {
        //List<ArchitecturalWork> aWorks = new List<ArchitecturalWork>();
        //for (int i = 0; i < 3; i++)
        //{
        //    architectWork = new ArchitecturalWork();
        //    aWorks.Add(architectWork);
        //}
        //grdArchitecturalWorks.DataSource = aWorks;
        //grdArchitecturalWorks.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (Request.QueryString["Id"] != null)
        {
            architect = new Architect(_dataObject); 
            architect.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
            architect.GetArchitectById();

            architect.Firm = txtFirmName.Text;
            if (uplArchitectImage.HasFile)
                architect.Logo = saveImage(uplArchitectImage);
            else
                architect.Logo = hfArchitectImageName.Value;
            architect.AddressLine = txtAddress.Text;
            architect.Phone = txtPhone.Text;
            architect.Fax = txtFax.Text;
            architect.Email = txtEmail.Text;
            architect.Website = txtWebsite.Text;
            architect.PropertyLocation = ddlPropertyLocation.SelectedItem.Value;
            architect.DescriptionLine = txtDescription.Text;
            architect.NoOfEmployees = txtEmployeeNos.Text;
            architect.FoundedYear = txtFounded.Text;
            architect.Specialization = txtSpecialization.Text;
            architect.Username = txtUsername.Text;
            architect.Password = txtPassword.Text;
            architect.DateUpdated = System.DateTime.Now;
            architect.Update();

            divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Architect updated successfully!";
            divMessage.Attributes.Add("class", "alert alert-success");
        }
        else
        {
            if (checkIfArchitectExists(txtUsername.Text))
            {
                divMessage.InnerHtml = "<strong>Oh snap!</strong>&nbsp;Architect with this username already exists!";
                divMessage.Attributes.Add("class", "alert alert-error");
            }
            else
            {
                architect = new Architect(_dataObject);
                architect.Firm = txtFirmName.Text;
                if (uplArchitectImage.HasFile)
                    architect.Logo = saveImage(uplArchitectImage);
                else
                    architect.Logo = hfArchitectImageName.Value;
                architect.AddressLine = txtAddress.Text;
                architect.Phone = txtPhone.Text;
                architect.Fax = txtFax.Text;
                architect.Email = txtEmail.Text;
                architect.Website = txtWebsite.Text;
                architect.PropertyLocation = ddlPropertyLocation.SelectedItem.Value;
                architect.DescriptionLine = txtDescription.Text;
                architect.NoOfEmployees = txtEmployeeNos.Text;
                architect.FoundedYear = txtFounded.Text;
                architect.Specialization = txtSpecialization.Text;
                architect.Username = txtUsername.Text;
                architect.Password = txtPassword.Text;
                architect.DateUpdated = System.DateTime.Now;
                architect.DateCreated = System.DateTime.Now;
                architect.Create();
                //architectWorks = GetGridDataForArchitectWorks();
                clearControls();
                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Architect added successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
        }
    }
    private bool checkIfArchitectExists(string userName)
    {
        Architect checkUser = new Architect(_dataObject);
        checkUser.Username = userName;
        checkUser.GetArchitectByUsername();

        if (checkUser.Id != 0)
            return true;
        else
            return false;
    }
    //private List<ArchitecturalWork> GetGridDataForArchitectWorks()
    //{
    //    for (int i = 0; i < grdArchitecturalWorks.Rows.Count; i++)
    //    {
    //        architectWork = new ArchitecturalWork(_dataObject);
    //        TextBox txtProjectName = (TextBox)grdArchitecturalWorks.Rows[i].FindControl("txtProjectName");
    //        TextBox txtDescriptionLine = (TextBox)grdArchitecturalWorks.Rows[i].FindControl("txtDescriptionLine");
    //        TextBox txtCityStateYear = (TextBox)grdArchitecturalWorks.Rows[i].FindControl("txtCityStateYear");

    //        if (!string.IsNullOrEmpty(txtProjectName.Text) && !string.IsNullOrEmpty(txtDescriptionLine.Text) && !string.IsNullOrEmpty(txtCityStateYear.Text))
    //        {
    //            architectWork.Name = txtProjectName.Text;
    //            architectWork.DescriptionLine = txtDescriptionLine.Text;
    //            architectWork.CityStateYear = txtCityStateYear.Text;
    //            architectWorks.Add(architectWork);
    //        }
    //    }

    //    return architectWorks;
    //}
    //private List<ArchitecturalFirm> GetGridDataForArchitectFirms()
    //{
        //for (int j = 0; j < grdArchitecturalFirms.Rows.Count; j++)
        //{
        //    architectFirm = new ArchitecturalFirm(_dataObject);
        //    TextBox txtArchitecturalFirmsName = (TextBox)grdArchitecturalFirms.Rows[j].FindControl("txtArchitecturalFirmsName");
        //    TextBox txtArchitecturalFirmsYear = (TextBox)grdArchitecturalFirms.Rows[j].FindControl("txtArchitecturalFirmsYear");

        //    if (!string.IsNullOrEmpty(txtArchitecturalFirmsName.Text) && !string.IsNullOrEmpty(txtArchitecturalFirmsYear.Text))
        //    {
        //        architectFirm.Name = txtArchitecturalFirmsName.Text;
        //        architectFirm.Year = txtArchitecturalFirmsYear.Text;
        //        architectFirms.Add(architectFirm);
        //    }
        //}

        //return architectFirms;
    //}
    private void clearControls()
    {
        txtFirmName.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtFax.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtWebsite.Text = string.Empty; 
        ddlPropertyLocation.SelectedIndex = -1;
        txtDescription.Text = string.Empty;
        txtEmployeeNos.Text = string.Empty;
        txtFounded.Text = string.Empty;
        txtSpecialization.Text = string.Empty;
        txtUsername.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }
    //protected void btnAddaWorks_Click(object sender, EventArgs e)
    //{
        //List<ArchitecturalWork> aWorks = new List<ArchitecturalWork>();
        //aWorks = GetGridDataForArchitectWorks();

        //architectWork = new ArchitecturalWork();
        //aWorks.Add(architectWork);

        //grdArchitecturalWorks.DataSource = aWorks;
        //grdArchitecturalWorks.DataBind();
    //}
    protected void btnAddaFirms_Click(object sender, EventArgs e)
    {
        //List<ArchitecturalFirm> aFirms = new List<ArchitecturalFirm>();
        //aFirms = GetGridDataForArchitectFirms();

        //architectFirm = new ArchitecturalFirm();
        //aFirms.Add(architectFirm);

        //grdArchitecturalFirms.DataSource = aFirms;
        //grdArchitecturalFirms.DataBind();
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        //bindCities();
    }
    private string saveImage(FileUpload fuImage)
    {
        try
        {
            string filename = Path.GetFileName(fuImage.FileName);
            string[] fileArray = filename.Split('.');
            if (!Directory.Exists(Server.MapPath(ConfigurationManager.AppSettings["ArchitectImagePath"].ToString())))
            {
                Directory.CreateDirectory(Server.MapPath(ConfigurationManager.AppSettings["ArchitectImagePath"].ToString()));
            }

            fuImage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["ArchitectImagePath"].ToString() + filename));
            return (filename);
        }
        catch (Exception ex)
        {
            return (ex.Message);
        }
    }
}
